package servidor;

//import cliente.gui.ServerInterface;
import servidor.auxiliar.GereServer;
import servidor.tcp.ConexaoServer;
import servidor.rmi.RMIservice;
import servidor.gui.ServerInterface;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.*;
import servidor.auxiliar.Arquivo;
import servidor.rmi.RMIConexao;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author A8
 */
public class Servidor {

    static int port = 7000;
    static int port_rmi = 8000;
    static String host = "localhost";

    public static void main(String args[]) {

        int opcao = 0;
        GereServer gestorServer = null;
        ServerInterface guiServer = new ServerInterface(opcao, gestorServer);


        guiServer.setVisible(false);
        guiServer.setVisible(true);

        /*
         * ----------------------------------------
         *  Ligar ao servidor SQL
         * ----------------------------------------
         */
        Connection connect = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            String Url = "jdbc:mysql://localhost/mydb?" + "user=root&password=user";
            connect = DriverManager.getConnection(Url);
        } catch (SQLException e) {
            System.out.println("SQL Exception: " + e.toString());
        } catch (ClassNotFoundException e) {
            System.out.println("Class Not Found Exception: " + e.toString());
        }
     

        //------------------------------------------

        opcao = guiServer.getOpcao();
        while (opcao == 0) {
            opcao = guiServer.getOpcao();
        }
        gestorServer = new GereServer();
        switch (guiServer.getOpcao()) {
            case 1:
                //TCP
                System.out.println("---TCP---");
                arrancaServidor(Servidor.port, gestorServer, guiServer, connect);
                break;
        }
        guiServer.setGestor(gestorServer);
    }

    public static void arrancaServidor(int port, GereServer gestorServer, ServerInterface guiServer, Connection connect) {

        try {
            ServerSocket listenSocket = new ServerSocket(port);
            System.out.println("A Escuta no Porto " + port);

            Arquivo clientes = new Arquivo();

            guiServer.setGestor(gestorServer);
            gestorServer.setConection(connect);
            gestorServer.setClientes(clientes);

            RMIservice servico_rmi = new RMIservice(port_rmi, host, gestorServer);
            RMIConexao rmi_liga = new RMIConexao(servico_rmi, gestorServer);
            System.out.println("[INFO]Ligacao RMI estabelecida!");

            while (true) {
                Socket client_tcp = listenSocket.accept();
                System.out.println("[INFO]Ligacao estabelecida com cliente!");
                ConexaoServer ligar = new ConexaoServer(client_tcp, connect, gestorServer);           
                gestorServer.setConexaoServer(ligar);
                ligar.setGestorServer(gestorServer);
                ligar.start();
            }

        } catch (IOException e) {
            System.out.println("[ERRO]IO:"+e.getMessage());
        }
    }
}
